using Godot;

public partial class GameManager : Node
{
    // 单例引用
    public static GameManager Instance { get; private set; }

    // 当前活跃的存档数据
    public SaveData CurrentSave { get; private set; }

    public override void _Ready()
    {
        Instance = this;
    }

    // 开始新游戏
    public void NewGame(string saveName)
    {
        CurrentSave = new SaveData
        {
            FileName = System.Guid.NewGuid().ToString(), // 生成唯一ID
            DisplayName = saveName,
            CurrentLevelPath = "res://levels/level_1.tscn",// 初始关卡路径
            LastPlayedTime = System.DateTime.Now.ToString()
        };

        SaveSystem.SaveGame(CurrentSave);
        LoadCurrentLevel();
    }

    // 加载已有存档
    public void LoadSave(SaveData save)
    {
        CurrentSave = save;
        LoadCurrentLevel();
    }

    // 进入关卡
    private void LoadCurrentLevel()
    {
        // GetTree().ChangeSceneToFile(CurrentSave.CurrentLevelPath);
        SceneTransition.Instance.ChangeScene(CurrentSave.CurrentLevelPath);
    }

    // 通关时调用：更新进度
    public void CompleteLevel(string nextLevelPath)
    {
        if (CurrentSave == null) return;

        CurrentSave.CurrentLevelPath = nextLevelPath;
        SaveSystem.SaveGame(CurrentSave); // 写入硬盘
    }
}